<?php

namespace App\Http\Controllers\AuthorizeApplets;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Models\WechatThirdConfig;

class AppletsController extends Controller
{
    protected $customerAppletsAuthorizeAppid = "customerapplets_authorize_appids";

    //第三方开放平台appid
    protected $app_id;
    //第三方开放平台secret
    protected $secret;
    //第三方开放平台token
    protected $token;
    //第三方开放平台aes_key
    protected $aes_key;
    //域名
    protected $domain;
    //微信公众号的appid
    protected $weChatServerAppId;
    //微信公众号的秘钥
    protected $weChatServerSecret;

    public function __construct()
    {
        $model = new WechatThirdConfig();
        $data = $model->getInfo();
        $this->app_id = $data->app_id;
        // $this->secret  = config('api.secret');
        $this->secret  = $data->app_secret;
        $this->token   = $data->token;
        $this->aes_key = $data->aes_key;
        $this->weChatServerAppId   = $data->weChatServerAppId;
        $this->weChatServerSecret  = $data->weChatServerSecret;
        $this->domain  = env('APP_URL');
    }

    /**
     * 授权小程序回调通知
     * @param $store_id
     *        门店id
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function refundAuthUrl($store_id, Request $request)
    {
        $requestAllData    = $request->all();
        $appletInfo = DB::table($this->customerAppletsAuthorizeAppid)->where(['t_appid' => $this->app_id,'applet_type' => 1,'AuthorizationCode' => $requestAllData['auth_code']])->first();
        if (!empty($appletInfo)) {
            $AuthorAppId = $appletInfo->AuthorizerAppid;
            $storeAppletInfo = DB::table("merchant_store_appid_secrets")->where(['store_id' => $store_id])->first();
            if (!empty($storeAppletInfo)) {
                DB::table("merchant_store_appid_secrets")->where(['store_id' => $store_id])->update([
                    'wechat_appid' => $AuthorAppId,
                    'updated_at'   => date("Y-m-d H:i:s", time())
                ]);
            } else {
                DB::table("merchant_store_appid_secrets")->insert([
                    'store_id'     => $store_id,
                    'wechat_appid' => $AuthorAppId,
                    'created_at'   => date("Y-m-d H:i:s", time()),
                    'updated_at'   => date("Y-m-d H:i:s", time()),
                ]);
            }
        }
        return view("webH5Mobile.authorizeSuccess");
    }

    /**
     * 输出授权小程序页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view("webH5Mobile.authorize");
    }

    /**
     * 输出创建小程序页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function createApplets()
    {
        return view("webH5Mobile.createApplets");
    }

    /**
     * 输出小程序管理列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function appletsList()
    {
        return view("webH5Mobile.appletList");
    }
}
